package cn.wolfcode.web.controller;


import cn.wolfcode.domain.MessageBoardHzh;
import cn.wolfcode.domain.MessageReplyHzh;
import cn.wolfcode.qo.MessageBoardQueryObject;
import cn.wolfcode.service.IMessageBoardHzhService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageReplyHzhService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

@Controller
@RequestMapping("/messageBoardHzh")
public class MessageBoardHzhController {

    @Autowired
    private IMessageBoardHzhService messageBoardHzhService;

    @Autowired
    private IMessageReplyHzhService messageReplyHzhService;

    @RequiredPermission(name="留言板页面",expermission="messageBoardHzh:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") MessageBoardQueryObject qo){
        PageInfo<MessageBoardHzh> pageInfo = messageBoardHzhService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "messageBoardHzh/list";
    }


    @RequiredPermission(name="留言板删除",expermission="messageBoardHzh:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           messageBoardHzhService.delete(id);
        }
        return "redirect:/messageBoardHzh/list";
    }


    @RequiredPermission(name="留言板新增/编辑",expermission="messageBoardHzh:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoardHzh messageBoardHzh) {
        if (messageBoardHzh.getId() == null) {
            messageBoardHzhService.save(messageBoardHzh);
        } else {
            messageBoardHzhService.update(messageBoardHzh);
        }
        return "redirect:/messageBoardHzh/list";
    }


    @RequiredPermission(name="留言板新增/编辑",expermission="messageBoardHzh:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            List<MessageReplyHzh> replyHzhs = messageReplyHzhService.get(id);
            model.addAttribute("replyHzhs",replyHzhs);
            model.addAttribute("messageBoardHzh",messageBoardHzhService.get(id));
        }
        return "messageBoardHzh/input";
    }
}
